import type { Effect, Reducer } from 'umi';
import { getGoodsInfo } from '@/pages/goods-details/service';
export interface GoodsInfoModelState {
  info: any;
  refreshGoodsCount: number;
}
export interface GoodsModelType {
  namespace: 'goods';
  state: GoodsInfoModelState;
  effects: {
    fetchRoleList: Effect;
  };
  reducers: {
    saveGoodsInfoList: Reducer<GoodsInfoModelState>;
  };
}

const GoodsModel: GoodsModelType = {
  namespace: 'goods',

  state: {
    info: {},
    refreshGoodsCount: 0,
  },

  effects: {
    *fetchRoleList({ payload }, { call, put }) {
      const response = yield call(getGoodsInfo, { id: payload.id });
      yield put({
        type: 'saveGoodsInfoList',
        payload: response.data,
      });
    },
  },
  reducers: {
    saveGoodsInfoList(state, action) {
      return {
        ...state,
        info: action.payload,
        refreshGoodsCount: state!.refreshGoodsCount + 1,
      };
    },
  },
};

export default GoodsModel;
